#!/bin/bash
# $1 srart_freq; $2 end_freq; $3 pll1 or pll2
start=0
if (($1));then
start=$1
else
        echo "Please input start frequency!"
fi

if (($2));then
end=$2
else
        echo "Please input end frequency!"
fi

vco_band=0x31d
pll=1
vco_reg=0x31c
if (($3)); then
        if (( $3 > 2 || $3 < 1)); then
                echo "PLL$3 is not exist!"
                exit 0;
        fi
        pll=$3
        if (( $pll == 2 ));then
                vco_band=0x329
                vco_reg=0x328
        fi
fi

dev_path="/sys/kernel/debug/iio/iio:device0/direct_reg_access"
if [ ! -f $dev_path ];then
	echo "Count not find dev!"
	exit 1
fi

function get_vco()
{
    if (( $1 )); then
                vco_reg=$1
        else
                exit 1
        fi

    echo "$vco_reg" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
    vco=$(cat /sys/kernel/debug/iio/iio:device0/direct_reg_access)
    let "vco&=0x60"
    let "vco>>=5"
    vco=$(printf %d $vco)
    echo "$vco"
}

get_sar()
{
        echo 0x3d0 "$1" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        echo 0x3e0 > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        sar_pow=$(cat /sys/kernel/debug/iio/iio:device0/direct_reg_access)
        let "sar_pow&=0xd7"
        echo 0x3e0 "$sar_pow" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        echo 0x3d2 > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        sar_start=$(cat /sys/kernel/debug/iio/iio:device0/direct_reg_access)
        let "sar_start|=0x0c"
        echo 0x3d2 "$sar_start" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        sar=$(cat /sys/bus/spi/devices/spi0.0/iio:device0/in_voltage2_raw)
        echo "$sar"
}

get_band()
{
        if (( $1 )); then
                vco_band=$1
        else
                exit 1
        fi
        echo "$vco_band" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        band=$(cat /sys/kernel/debug/iio/iio:device0/direct_reg_access)
        let "band&=0x3f"
        band=$(printf %d $band)
        echo $band
}

function get_more()
{
    # echo "$1 $2 $3"
    if (( ! $1 || ! $2 || ! $3 )); then
        exit 1
    else
        pll=$1
        band_reg=$2
        band=$3
    fi

    if ((band-4>0));then
        let "band-=4"
    else
        band=0
    fi
    
    while ((band < $3 + 4))
    do
        echo "$band_reg $band" > /sys/kernel/debug/iio/iio:device0/direct_reg_access
        sar=`get_sar $pll`
        # echo "$band_reg $band $sar"
        if [ $sar -gt 250 ]; then
            break
        else
                if [ $sar -ge 10 ]; then
                        echo "$4,$band,$sar,1,$5" >> "loinfo.csv"
                fi
        fi
        let "band++"
    done
}

rm loinfo.csv > /dev/null 2>&1
i=0
while ((i<1))
do
        echo "freq,band,sar,flag,vco"
        echo "freq,band,sar,flag,vco" >> "loinfo.csv"
        freq=$start
        while (($freq<=$end))
        do
                echo "$freq" > /sys/kernel/debug/iio/iio:device0/avc,retune_pll"$pll"
                echo "$pll" > /sys/kernel/debug/iio/iio:device0/avc,lock_flag_pll
                flag=$(cat /sys/kernel/debug/iio/iio:device0/avc,lock_flag_pll)

                band=`get_band $vco_band`
                sar=`get_sar $pll`
                vco=`get_vco $vco_reg`
                echo "$freq,$band,$sar,$flag,$vco"
        #       echo "$freq,$sar,$band,$flag,$vco" >> "loinfo.csv"
                get_more $pll $vco_band $band $freq $vco
                let freq=$freq+5000000
        done
        let i=$i+1
done
